package com.yyk.yiliao.ui.mine.activity;


import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.ui.home.activity.AddMember_Activity;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.MymessageVisitlisInfo;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的 就诊人员   可以查看
 */
public class PersonnelSelection2_Activity extends BaseActivity {


    @BindView(R.id.mMember_tishi)
    TextView mMemberTishi;
    @BindView(R.id.mMember_rv)
    RecyclerView mMemberRv;
    private List<MymessageVisitlisInfo.DataBean> memberList = new ArrayList<>();
    private BaseRecyclerAdapter<MymessageVisitlisInfo.DataBean> adapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_personnel_selection2;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setMainTitleRightText("添加", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(AddMember_Activity.class);
            }
        });
        setTitle("就诊成员管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPost();
    }

    @Override
    public void initData() {
        initAdapter();


    }

    private void initPost() {
        initMymessageVisitlis(true);
    }

    /**
     * 人员列表
     *
     * @param flag
     */
    private void initMymessageVisitlis(final boolean flag) {
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id", Hawk.get("uid", 0) + "");
        RxUtils.getInstance().retrofit.create(ApiService.class).postMymessageVisitlis(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<MymessageVisitlisInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MymessageVisitlisInfo info) {
                        Logger.e("人员选择" + info.toString());

                        if (info.getCode() == 1) {
                            mMemberRv.setVisibility(View.VISIBLE);
                            mMemberTishi.setVisibility(View.GONE);
                            if (flag) {
                                memberList.clear();
                            }

                            if (info.getData().size() > 0 && info.getData() != null) {
                                memberList.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            mMemberRv.setVisibility(View.GONE);
                            mMemberTishi.setVisibility(View.VISIBLE);
                        }
                    }

                });
    }

    /**
     * 查看 成员信息
     */
    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<MymessageVisitlisInfo.DataBean>(mActivity, memberList, R.layout.adapter_item_personnelmessage2) {
            @Override
            public void convert(BaseRecyclerHolder holder, final MymessageVisitlisInfo.DataBean data, int position, boolean isScrolling) {
                holder.setText(R.id.b_real_name, data.getReal_name());
                holder.getView(R.id.b_see).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String real_name = data.getReal_name();
                        int patient_id = data.getPatient_id();
                        int status = data.getStatus();
//                        if (status == 1) {
                        Bundle bundle = new Bundle();
                        bundle.putString("patient_id", patient_id + "");
                        bundle.putString("status", status + "");
                        startAct(QueryMember_Activity.class, bundle);
//                        }

                    }
                });

            }
        };
        mMemberRv.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_eb_1px, 0));
        mMemberRv.setLayoutManager(new LinearLayoutManager(mActivity));
        mMemberRv.setAdapter(adapter);

    }


}
